package cn.iocoder.yudao.module.trade.aftersale.service;

import cn.iocoder.yudao.module.trade.aftersale.bo.AfterSaleLogCreateReqBO;
import cn.iocoder.yudao.module.trade.aftersale.convert.AfterSaleLogConvert;
import cn.iocoder.yudao.module.trade.aftersale.dal.dataobject.AfterSaleDO;
import cn.iocoder.yudao.module.trade.aftersale.dal.dataobject.AfterSaleLogDO;
import cn.iocoder.yudao.module.trade.aftersale.dal.mysql.AfterSaleLogMapper;
import cn.iocoder.yudao.module.trade.aftersale.dal.mysql.AfterSaleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.trade.ErrorCodeConstants.AFTER_SALE_NOT_FOUND;

/**
 * 售后订单日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AfterSaleLogServiceImpl implements AfterSaleLogService {

    @Resource
    private AfterSaleLogMapper afterSaleLogMapper;
    @Resource
    private AfterSaleMapper afterSaleMapper;

    @Override
    public void createAfterSaleLog(AfterSaleLogCreateReqBO createReqBO) {
        AfterSaleDO afterSale = validateAfterSale(createReqBO.getAfterSaleId());
        AfterSaleLogDO afterSaleLog = AfterSaleLogConvert.INSTANCE.convert(createReqBO);
        afterSaleLog.setShopId(afterSale.getShopId());
        afterSaleLogMapper.insert(afterSaleLog);
    }

    @Override
    public List<AfterSaleLogDO> getAfterSaleLogList(Long afterSaleId) {
        return afterSaleLogMapper.selectListByAfterSaleId(afterSaleId);
    }

    private AfterSaleDO validateAfterSale(Long afterSaleId) {
        AfterSaleDO afterSale = afterSaleMapper.selectById(afterSaleId);
        if (afterSale == null) {
            throw exception(AFTER_SALE_NOT_FOUND);
        }
        return afterSale;
    }

}
